package com.github.xuemingyu.springboot.acticitycloud.controller.api;

import com.bidanet.bdcms.core.bean.ApiResult;
import com.bidanet.bdcms.core.vo.Page;
import com.github.xuemingyu.springboot.acticitycloud.entity.ActivityTemplate;
import com.github.xuemingyu.springboot.acticitycloud.service.ActivityTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/model")
public class ActivityTemplateApiController {
    @Autowired
    private ActivityTemplateService activityTemplateService;

    @RequestMapping("/query")
    public ApiResult index(ActivityTemplate activityTemplate, Page page) {
        try {
            activityTemplateService.queryLike(activityTemplate, page);
        } catch (Exception e) {
            return ApiResult.error(e.getMessage());
        }
        return ApiResult.success(page);
    }

    @RequestMapping("/findOne")
    public ApiResult findOne(Long modelId) {
        ActivityTemplate oneByModelId = null;
        try {
            oneByModelId = activityTemplateService.findOneByModelId(modelId);
        } catch (Exception e) {
            return ApiResult.error(e.getMessage());
        }
        return ApiResult.success(oneByModelId);
    }

    @RequestMapping("/add")
    public ApiResult insert(ActivityTemplate activityTemplate, HttpServletRequest request) {
        try {
            activityTemplate.setUserId(request.getHeader("userId"));
            activityTemplateService.insert(activityTemplate);
        } catch (Exception e) {
            return ApiResult.error(e.getMessage());
        }
        return ApiResult.success("添加成功");
    }

    @RequestMapping("/modify")
    public ApiResult update(ActivityTemplate activityTemplate) {
        try {
            activityTemplateService.update(activityTemplate);
        } catch (Exception e) {
            return ApiResult.error(e.getMessage());
        }
        return ApiResult.success("编辑成功");
    }

    @RequestMapping("/delete")
    public ApiResult delete(String ids) {
        activityTemplateService.delete(ids);
        return ApiResult.success("删除成功");
    }
}
